package com.fone.player.wxapi;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.activity.pay.FoneThirdPayActivity;
import com.fone.player.billing.IFonePayManager;
import com.fone.player.billing.impl.FonePayManager;
import com.fone.player.billing.util.FoneConstants;
import com.fone.player.billing.util.common.PayEngine;
import com.fone.player.sns.UserInfoManager;
import com.fone.player.util.ActivityQueue;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;

	private TextView tv_pay_msg,wx_button_tv,tv_wx_pay_title;
	private boolean wx_success;
	private IFonePayManager mFonePayManger;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        View view = findViewById(R.layout.pay_result);
//        WindowManager.LayoutParams params0 = getWindow().getAttributes();
//        params0.width = (int) (ScreenUtil
//				.getScreenWidthPix(WXPayEntryActivity.this) * 0.8*0.9);   
//        params0.height = (int) (LayoutParams.WRAP_CONTENT); 
//        getWindow().setAttributes(params0);
//quit.getWindow().setAttributes(params0);
//        getWindow().setLayout((int) (ScreenUtil
//				.getScreenWidthPix(WXPayEntryActivity.this) * 0.8), 1000);

        setContentView(R.layout.pay_result);
        L.i(TAG, "oncreate>>");
        initView();
        initData();
    	api = WXAPIFactory.createWXAPI(this, FoneConstants.app_ID );
        api.handleIntent(getIntent(), this);
        
        
    }

	private void initData() {
		mFonePayManger = FonePayManager.getInstance();
	}

	private void initView() {
		L.i(TAG, "WXPayEntryActivity", "init textview tv_pay_msg");
		tv_pay_msg = (TextView) findViewById(R.id.tv_wx_pay_msg);
		wx_button_tv = (TextView) findViewById(R.id.tv_wx_button);
		tv_wx_pay_title = (TextView) findViewById(R.id.tv_wx_pay_title);
		
		tv_wx_pay_title.setText("微信支付结果");
		wx_button_tv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				backfromWxPay();
			}
		});
	}

	@Override
	public void onReq(BaseReq req) {
		L.i(TAG, "onPayStart, openId = " + req.openId);
		L.i(TAG, "onPayStart, transaction = " + req.transaction);
		L.i(TAG, "onPayStart, getType = " + req.getType());
		L.i(TAG, "onPayStart, checkArgs = " + req.checkArgs());
	}

	@Override
	public void onResp(BaseResp resp) {
		L.i(TAG, "onPayFinish, errCode = " + resp.errStr);
		L.i(TAG, "onPayFinish, errCode = " + resp.errCode);
		L.i(TAG, "onPayFinish, errStr = " + resp.errStr);
		L.i(TAG, "onPayFinish, openId = " + resp.openId);
		L.i(TAG, "onPayFinish, transaction = " + resp.transaction);
		L.i(TAG, "onPayFinish, getType = " + resp.getType());
		L.i(TAG, "onPayFinish, checkArgs = " + resp.checkArgs());
		// int errCode = resp.errCode;
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if (null != resp) {
				SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ApplicationManage.getGlobalContext());
				String wxrurl = settings.getString("wxrurl", "");
				String wxtof = settings.getString("wxtof", "");
				int wxty = settings.getInt("wxtype", -1);
				SharedPreferences.Editor editor = settings.edit();
				editor.putString("wxrurl", "");
				editor.putString("wxtof","");
				editor.putInt("wxtype",-1);
				editor.commit();
				L.i(TAG, "" + resp.errCode);
				if(0!=resp.errCode){
					tv_pay_msg.setText("支付失败");
					if(null != wxrurl && !"".equals(wxrurl)){
						wxrurl = PayEngine.getNotifyUrl(wxrurl, ""+resp.errCode, 1, wxtof, "");
					}
				}else{
//					IFonePayManager mFonePayManger = FonePayManager.getInstance();
//					mFonePayManger.getUserFeeNotif();
					if(wxty == FoneConstants.DO_VIP_ACTION){
						UserInfoManager.saveVipType("1");
					}
					wx_success = true;
					tv_pay_msg.setText("支付成功");
					if(null != wxrurl && !"".equals(wxrurl)){
						wxrurl = PayEngine.getNotifyUrl(wxrurl, ""+resp.errCode, 0, wxtof, "");
					}
				}
				L.i(TAG, "微信客户端上报地址："+wxrurl);
				if(null != wxrurl && !"".equals(wxrurl) && !wxrurl.contains("null?")){
					notifyFeeServer(wxrurl);
				}
			}
		}
	}
	
	protected void notifyFeeServer(final String notifyUrl) {
		new Thread(
				new Runnable() {
					
					@Override
					public void run() {
						PayEngine.getNotifyInfo(notifyUrl, null, PayEngine.GET);
					}
				}).start();
		
	}
	
	@Override
	public void onBackPressed() {
		backfromWxPay();
	}

	private void backfromWxPay() {
		
		finish();
		L.i(TAG, ""+wx_success);
		if(wx_success){
			if (UserInfoManager.isLogin() && FoneUtil.isNetOkWithToast(ApplicationManage.getGlobalContext())) {

				L.i(TAG, "用户登录 获取VIP状态");
				new Thread(new Runnable() {

					@Override
					public void run() {
						mFonePayManger.getUserFeeNotif();
					}
				}).start();
			} else {
				L.i(TAG, "用户未登录 退出计费");
			}
			
			Activity currentActivity = ActivityQueue.popIndex(0);
			if(null != currentActivity && currentActivity instanceof FoneThirdPayActivity){
				currentActivity.finish();
			}
		}
	}
}